//
//  FLBaseClass.m
//
//  Created by   on 2019/2/23
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "FLBaseClass.h"
#import "FLData.h"


NSString *const kFLBaseClassStatus = @"status";
NSString *const kFLBaseClassData = @"data";


@interface FLBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation FLBaseClass

@synthesize status = _status;
@synthesize data = _data;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.status = [self objectOrNilForKey:kFLBaseClassStatus fromDictionary:dict];
    NSObject *receivedFLData = [dict objectForKey:kFLBaseClassData];
    NSMutableArray *parsedFLData = [NSMutableArray array];
    
    if ([receivedFLData isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedFLData) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedFLData addObject:[FLData modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedFLData isKindOfClass:[NSDictionary class]]) {
       [parsedFLData addObject:[FLData modelObjectWithDictionary:(NSDictionary *)receivedFLData]];
    }

    self.data = [NSArray arrayWithArray:parsedFLData];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.status forKey:kFLBaseClassStatus];
    NSMutableArray *tempArrayForData = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.data) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForData addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForData] forKey:kFLBaseClassData];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.status = [aDecoder decodeObjectForKey:kFLBaseClassStatus];
    self.data = [aDecoder decodeObjectForKey:kFLBaseClassData];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_status forKey:kFLBaseClassStatus];
    [aCoder encodeObject:_data forKey:kFLBaseClassData];
}

- (id)copyWithZone:(NSZone *)zone {
    FLBaseClass *copy = [[FLBaseClass alloc] init];
    
    
    
    if (copy) {

        copy.status = [self.status copyWithZone:zone];
        copy.data = [self.data copyWithZone:zone];
    }
    
    return copy;
}


@end
